/*
 * @lc app=leetcode id=46 lang=cpp
 *
 * [46] Permutations
 */
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> ret;
        loop(nums, 0, ret);
        return ret;
    }

    void loop(vector<int>& nums, int idx, vector<vector<int>>& ret) {
        int n = nums.size();
        if (idx == n) {
            ret.push_back(nums);
            return ;
        }

        for (int i = idx; i < n; i++) {
            swap(nums[i], nums[idx]);
            loop(nums, idx+1, ret);
            swap(nums[i], nums[idx]);
        }
    }
};

